package com.xzakota.minesweeper.widget.menu;

import com.xzakota.minesweeper.controller.Game;
import com.xzakota.minesweeper.model.GameModel;
import com.xzakota.minesweeper.model.entity.LevelBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 单人游戏菜单
 *
 * @author Xzakota
 */
public class SinglePlayerGameMenuBar extends JMenuBar implements ActionListener {
    public SinglePlayerGameMenuBar(GameModel model) {
        createGameMenu(model);
        createHelpMenu();
        allMenuItemAddListener();
    }

    private void allMenuItemAddListener() {
        Component[] menus = this.getComponents();
        JMenu menu;
        Component[] menuItems;
        for (Component component : menus) {
            menu = (JMenu) component;
            menuItems = menu.getMenuComponents();
            for (Component menuItem : menuItems) {
                if (menuItem instanceof JMenuItem) {
                    ((JMenuItem) menuItem).addActionListener(this);
                }
            }
        }
    }

    private void createGameMenu(GameModel config) {
        JMenu gameMenu = new JMenu("游戏(G)");
        gameMenu.setMnemonic('G');
        add(gameMenu);

        JMenuItem newGameMI = new JMenuItem("开局(N)", 'N');
        newGameMI.setAccelerator(KeyStroke.getKeyStroke("F2"));
        newGameMI.setActionCommand("newGame");
        gameMenu.add(newGameMI);

        gameMenu.addSeparator();

        JMenuItem levelBasicMI = new JCheckBoxMenuItem("初级(B)");
        levelBasicMI.setMnemonic('B');
        levelBasicMI.setActionCommand("setBasicLevel");
        levelBasicMI.setSelected(config.getCurrentGameLevelID() == LevelBean.ID_BASIC);
        gameMenu.add(levelBasicMI);

        JMenuItem levelIntermediateMI = new JCheckBoxMenuItem("中级(I)");
        levelIntermediateMI.setMnemonic('I');
        levelIntermediateMI.setActionCommand("setIntermediateLevel");
        levelIntermediateMI.setSelected(config.getCurrentGameLevelID() == LevelBean.ID_INTERMEDIATE);
        gameMenu.add(levelIntermediateMI);

        JMenuItem levelExpenseMI = new JCheckBoxMenuItem("高级(E)");
        levelExpenseMI.setMnemonic('E');
        levelExpenseMI.setActionCommand("setExpenseLevel");
        levelExpenseMI.setSelected(config.getCurrentGameLevelID() == LevelBean.ID_EXPENSE);
        gameMenu.add(levelExpenseMI);

        JMenuItem levelCustomMI = new JCheckBoxMenuItem("自定义(C)...");
        levelCustomMI.setMnemonic('C');
        levelCustomMI.setActionCommand("showCustomLevelDialog");
        levelCustomMI.setSelected(config.getCurrentGameLevelID() == LevelBean.ID_CUSTOM);
        gameMenu.add(levelCustomMI);

        ButtonGroup levelButtonGroup = new ButtonGroup();
        levelButtonGroup.add(levelBasicMI);
        levelButtonGroup.add(levelIntermediateMI);
        levelButtonGroup.add(levelExpenseMI);
        levelButtonGroup.add(levelCustomMI);

        gameMenu.addSeparator();

        JMenuItem toggleQuestionFlagMI = new JCheckBoxMenuItem("标记(?)(M)");
        toggleQuestionFlagMI.setMnemonic('M');
        toggleQuestionFlagMI.setActionCommand("toggleQuestionMarkEnable");
        toggleQuestionFlagMI.setSelected(config.isQuestionMarkEnable());
        gameMenu.add(toggleQuestionFlagMI);

        JMenuItem toggleSoundMI = new JCheckBoxMenuItem("声音(S)");
        toggleSoundMI.setMnemonic('S');
        toggleSoundMI.setActionCommand("toggleSoundEnable");
        toggleSoundMI.setSelected(config.isSoundEnable());
        gameMenu.add(toggleSoundMI);

        gameMenu.addSeparator();

        JMenuItem sweepHeroesListMI = new JMenuItem("扫雷英雄榜(T)...", 'T');
        gameMenu.add(sweepHeroesListMI);
        sweepHeroesListMI.setActionCommand("showHeroListDialog");

        gameMenu.addSeparator();

        JMenuItem exitMI = new JMenuItem("退出(X)", 'X');
        exitMI.setActionCommand("exitingGame");
        gameMenu.add(exitMI);
    }

    private void createHelpMenu() {
        JMenu helpMenu = new JMenu("帮助(H)");
        helpMenu.setMnemonic('H');
        add(helpMenu);

        JMenuItem helpMI = new JMenuItem("使用帮助(H)", 'H');
        helpMI.setAccelerator(KeyStroke.getKeyStroke("F1"));
        helpMI.setActionCommand("showHelpDialog");
        helpMenu.add(helpMI);

        helpMenu.addSeparator();

        JMenuItem aboutMI = new JMenuItem("关于扫雷(A)...", 'A');
        aboutMI.setActionCommand("showAboutDialog");
        helpMenu.add(aboutMI);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Game.dispatchEvent(event.getActionCommand());
    }
}
